/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.commons;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.Properties;

/**
 * Persist
 * 
 * @author Bernard Bou
 */
public class Persist
{
	/**
	 * Obtain settings (handles initial state)
	 * 
	 * @param thisPersistFile
	 *        persist file
	 * @return properties
	 */
	static public Properties getSettings(String thisPersistFile)
	{
		Properties theseSettings = Persist.loadSettings(thisPersistFile);
		if (theseSettings.size() == 0)
		{
			String thisLocation = CodeBase.getJarLocation();
			if (thisLocation == null)
			{
				File thisFile = new File(System.getProperty("user.dir")); //$NON-NLS-1$
				try
				{
					thisLocation = thisFile.toURI().toURL().toString();
				}
				catch (MalformedURLException thisException)
				{
					return theseSettings;
				}
			}
			if (!thisLocation.endsWith("/")) //$NON-NLS-1$
				thisLocation += '/';
			if (thisLocation.endsWith("/lib/")) //$NON-NLS-1$
				thisLocation = thisLocation.substring(0, thisLocation.length() - 4);
			theseSettings.setProperty("base", thisLocation + "data/test/"); //$NON-NLS-1$ //$NON-NLS-2$
			theseSettings.setProperty("images", thisLocation + "data/test/images/"); //$NON-NLS-1$ //$NON-NLS-2$
			theseSettings.setProperty("help", thisLocation + "doc/"); //$NON-NLS-1$ //$NON-NLS-2$
			theseSettings.setProperty("browser", "firefox"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		return theseSettings;
	}

	/**
	 * Load properties from file
	 * 
	 * @param thisPersistFile
	 *        persist file
	 * @return properties
	 */
	static protected Properties loadSettings(String thisPersistFile)
	{
		Properties theseSettings = new Properties();

		try
		{
			String thisFilePath = System.getProperty("user.home") + File.separator + "." + thisPersistFile; //$NON-NLS-1$ //$NON-NLS-2$
			InputStream thisPropStream = new FileInputStream(thisFilePath);
			theseSettings.load(thisPropStream);
			return theseSettings;
		}
		catch (Exception e)
		{
		}
		try
		{
			String thisFilePath = System.getProperty("user.dir") + File.separator + "." + thisPersistFile; //$NON-NLS-1$ //$NON-NLS-2$
			InputStream thisPropStream = new FileInputStream(thisFilePath);
			theseSettings.load(thisPropStream);
			return theseSettings;
		}
		catch (Exception e)
		{
		}
		return theseSettings;
	}

	/**
	 * Save persist data
	 * 
	 * @param thisPersistFile
	 *        persist file
	 * @param theseSettings
	 *        settings to persist
	 * @return true if successful
	 */
	static public boolean saveSettings(String thisPersistFile, Properties theseSettings)
	{
		try
		{
			String thisFilePath = System.getProperty("user.home") + File.separator + "." + thisPersistFile; //$NON-NLS-1$ //$NON-NLS-2$
			OutputStream thisPropStream = new FileOutputStream(thisFilePath);
			theseSettings.store(thisPropStream, "jFamilyTree"); //$NON-NLS-1$
			return true;
		}
		catch (Exception e)
		{
			System.out.println("Cannot save persist file :" + e); //$NON-NLS-1$
		}
		return false;
	}
}
